using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Aop;
using log4net;

namespace Com.Pirate.Common.Utils.ExceptionHandler
{
    public class ExceptionAdvice : IThrowsAdvice
    {
        private static readonly ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public void AfterThrowing(Exception ex)
        {
            if (ex.StackTrace.Contains("Infrastructure"))
            {
                logger.Fatal(ex.Source+" Infrastructure Layer Exception: " + ex.Message);
                logger.Error(ex.StackTrace);
                throw new InfrastructureException("Infrastructure Layer Exception: ", ex, ex.Source);
            }
            else if (ex.StackTrace.Contains("Domain.Services"))
            {
                logger.Fatal(ex.Source + " Domain.Services Layer Exception: " + ex.Message);
                logger.Error(ex.StackTrace);
                throw new DomainServiceException("Domain.Services Layer Exception: ", ex, ex.Source);
            }
            else if (ex.StackTrace.Contains("App.Services"))
            {
                logger.Fatal(ex.Source + " App.Services Layer Exception: " + ex.Message);
                logger.Error(ex.StackTrace);
                throw new AppServiceException("App.Services Layer Exception: ", ex, ex.Source);
            }
            else
            {
                Console.WriteLine(ex.Message);
                //throw ex;
            }
        }
    }
}
